package managedbean.IT;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;
import javax.ejb.EJB;
import session.stateless.AccessControlSessionBeanLocal;
import session.stateless.AccessRoleSessionBeanLocal;
import javax.faces.bean.ManagedProperty;
import entity.AccessControl;
import entity.AccessRole;
import java.util.ArrayList;
import managedbean.UserSessionManagerBean;
import javax.faces.context.FacesContext;
import java.util.Collections;
import javax.faces.application.FacesMessage;
import session.stateless.LogSessionBeanLocal;

@ManagedBean(name = "assignControlManagerBean")
@RequestScoped
public class AssignControlManagerBean {

    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    @EJB
    private AccessControlSessionBeanLocal accessControlSessionBeanLocal;
    @EJB
    private AccessRoleSessionBeanLocal accessRoleSessionBeanLocal;
    @EJB
    private LogSessionBeanLocal logSessionBeanLocal;
    private ArrayList<String> controlsCurrent;
    private ArrayList<String> roles;
    private ArrayList<String> controlsAvailable;
    private String roleType;
    private String description;
    private String selectAdd;
    private String selectRemove;
    private String loadAssignControl;

    public String getLoadAssignControl() {

        try {
            updateControlList();
        } catch (Exception exception) {
            try {

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort()
                        + "/MerlionERP-war/Web_IT/searchRole.xhtml?request=assignControl");
            } catch (Exception ex) {
            }
        }

        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="Getter & Setter methods. Click on the + sign on the left to edit the code.">
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    public AccessControlSessionBeanLocal getAccessControlSessionLocal() {
        return accessControlSessionBeanLocal;
    }

    public void setAccessControlSessionLocal(AccessControlSessionBeanLocal accessControlSessionBeanLocal) {
        this.accessControlSessionBeanLocal = accessControlSessionBeanLocal;
    }

    public AccessRoleSessionBeanLocal getAccessRoleSessionBeanLocal() {
        return accessRoleSessionBeanLocal;
    }

    public void setAccessRoleSessionBeanLocal(AccessRoleSessionBeanLocal accessRoleSessionBeanLocal) {
        this.accessRoleSessionBeanLocal = accessRoleSessionBeanLocal;
    }

    public ArrayList<String> getControlsCurrent() {
        return controlsCurrent;
    }

    public void setControlsCurrent(ArrayList<String> controlsCurrent) {
        this.controlsCurrent = controlsCurrent;
    }

    public ArrayList<String> getRoles() {
        return roles;
    }

    public void setRoles(ArrayList<String> roles) {
        this.roles = roles;
    }

    public ArrayList<String> getControlsAvailable() {
        return controlsAvailable;
    }

    public void setControlsAvailable(ArrayList<String> controlsAvailable) {
        this.controlsAvailable = controlsAvailable;
    }

    public String getSelectAdd() {
        return selectAdd;
    }

    public void setSelectAdd(String selectAdd) {
        this.selectAdd = selectAdd;
    }

    public String getSelectRemove() {
        return selectRemove;
    }

    public void setSelectRemove(String selectRemove) {
        this.selectRemove = selectRemove;
    }

    public String getRoleType() {
        return roleType;
    }

    public void setRoleType(String roleType) {
        this.roleType = roleType;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setLoadAssignControl(String loadAssignControl) {
        this.loadAssignControl = loadAssignControl;
    }
    //</editor-fold>

    public void updateControlList() {
        AccessRole accessRole = (AccessRole) userSessionManagerBean.getSelectedItem();
        roleType = accessRole.getRoleType();
        description = accessRole.getDescription();

        setControlsCurrent(accessRoleSessionBeanLocal.getControls(roleType));
        setControlsAvailable(new ArrayList<String>());

        boolean found = false;
        for (AccessControl a : accessControlSessionBeanLocal.getAccessControls()) {
            for (int i = 0; i < controlsCurrent.size(); i++) {

                if (a.getTitle().compareTo(controlsCurrent.get(i)) == 0) {
                    found = true;
                } else {
                }

            }
            if (!found) {
                getControlsAvailable().add(a.getTitle());
            }
            found = false;
        }

        Collections.sort(getControlsAvailable());

    }

    public void doneUpdate(ActionEvent event) {
      
        if (controlsCurrent.size() > 0) {
            try {
                ArrayList<String> al = new ArrayList<String>();
                al.add("Control assignment completed");
                al.add("You have successfully assigned/removed control from the role. Please be reminded that the changes will only be reflected on the next login.");
                al.add("Click on the link to return to ");
                al.add("continue");
                al.add("Web_IT/searchRole.xhtml?request=assignControl");
                userSessionManagerBean.setSelectedItem(al);

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort() + "/MerlionERP-war/success.xhtml");

            } catch (Exception ex) {
                
            }
        } else {
         
             FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Access role need at least 1 control.", ""));
        }

    }

    public void addControl(ActionEvent event) {
        if (selectAdd != null) {
            accessRoleSessionBeanLocal.updateAccessControl(roleType, selectAdd, true);
            logSessionBeanLocal.addLog("Assign access control", "Access control: " + selectAdd + " assigned to access control: " + roleType, "assignControl.xhtml", userSessionManagerBean.getUserId());

        }
        updateControlList();
    }

    public void removeControl(ActionEvent event) {
        if (selectRemove != null && controlsCurrent.size() > 0) {
            accessRoleSessionBeanLocal.updateAccessControl(roleType, selectRemove, false);
            logSessionBeanLocal.addLog("Assign access control", "Access control: " + selectAdd + " unassigned from access control: " + roleType, "assignControl.xhtml", userSessionManagerBean.getUserId());
        }
        updateControlList();

    }
}